<template>
  <div class="pet-commodity-list">
    <el-row :gutter="20">
      <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="item in props.commodityList" :key="item.id">
        <el-card
            class="pet-card"
            :body-style="{ padding: '0px' }"
            shadow="hover"
            @click="goCommodityDetail(item.id)"
        >
          <div class="pet-image-container">
            <el-image
                :src="item.commodityAvatar"
                fit="cover"
                class="pet-image"
                :preview-src-list="[item.commodityAvatar]"
            >
              <template #error>
                <div class="image-error">
                  <el-icon><Picture /></el-icon>
                </div>
              </template>
            </el-image>
            <div class="pet-badge" v-if="item.isNew">
              <el-tag type="danger" effect="dark" size="small">新品</el-tag>
            </div>
          </div>

          <div class="pet-info">
            <div class="pet-header">
              <h3 class="pet-title">{{ item.commodityName }}</h3>
              <div class="pet-price">
                <span class="current-price">¥{{ item.price }}</span>
              </div>
            </div>

            <p class="pet-description">{{ item.commodityDescription }}</p>

            <div class="pet-meta">
              <div class="pet-tags">
                <el-tag
                    v-if="item.commodityTypeName"
                    size="small"
                    effect="plain"
                >
                  {{ item.commodityTypeName }}
                </el-tag>

                <el-tag
                    type="success"
                    size="small"
                    effect="plain"
                    v-if="item.commodityInventory > 0"
                >
                  库存: {{ item.commodityInventory }}
                </el-tag>
                <el-tag
                    type="danger"
                    size="small"
                    effect="plain"
                    v-else
                >
                  已售罄
                </el-tag>
              </div>

              <div class="pet-stats">
                <div class="stat-item">
                  <el-icon :size="14" color="#999"><View /></el-icon>
                  <span>{{ item.viewNum || 0 }}</span>
                </div>
                <div class="stat-item">
                  <el-icon :size="14" color="#ff6b6b"><Star /></el-icon>
                  <span>{{ item.favourNum || 0 }}</span>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { defineProps } from "vue";
import { useRouter } from "vue-router";
import { ElTag, ElIcon, ElImage } from "element-plus";
import { View, Star, Picture } from "@element-plus/icons-vue";

const $router = useRouter();
const props = defineProps({
  commodityList: {
    type: Array,
    required: true,
    default: () => []
  }
});

// 跳转到商品详情页
const goCommodityDetail = (id: number) => {
  $router.push("/user/commodity/detail/" + id);
};


</script>

<style scoped lang="scss">
.pet-commodity-list {
  padding: 20px;

  .pet-card {
    margin-bottom: 20px;
    border-radius: 12px;
    overflow: hidden;
    transition: all 0.3s ease;
    border: none;
    cursor: pointer;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
    }
  }

  .pet-image-container {
    position: relative;
    width: 100%;
    height: 180px;
    overflow: hidden;

    .pet-image {
      width: 100%;
      height: 100%;
      transition: transform 0.5s ease;

      &:hover {
        transform: scale(1.05);
      }
    }

    .image-error {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #f5f5f5;
      color: #ccc;
    }

    .pet-badge {
      position: absolute;
      top: 10px;
      right: 10px;
      z-index: 1;
    }
  }

  .pet-info {
    padding: 16px;

    .pet-header {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      margin-bottom: 10px;

      .pet-title {
        margin: 0;
        font-size: 16px;
        font-weight: 600;
        color: #333;
        flex: 1;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .pet-price {
        margin-left: 10px;
        text-align: right;

        .current-price {
          font-size: 18px;
          font-weight: bold;
          color: #ff6b6b;
        }

        .original-price {
          font-size: 12px;
          color: #999;
          text-decoration: line-through;
          margin-left: 4px;
        }
      }
    }

    .pet-description {
      font-size: 14px;
      color: #666;
      line-height: 1.5;
      margin: 8px 0;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 42px;
    }

    .pet-meta {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 12px;

      .pet-tags {
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
      }

      .pet-stats {
        display: flex;
        gap: 12px;

        .stat-item {
          display: flex;
          align-items: center;
          font-size: 12px;
          color: #999;

          span {
            margin-left: 4px;
          }
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .pet-commodity-list {
    .pet-card {
      margin-bottom: 15px;
    }

    .pet-image-container {
      height: 150px;
    }
  }
}
</style>
